import { Injectable } from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {FormUtil} from "../../../util/form-util";
import {Observable} from "rxjs";
import {MsgResult} from "../../../models/msg-result";
import {SiteV2API} from "../site-v2-constants";
import {SiteDevices} from "./site-devices";


@Injectable()
export class SiteDevicesService {

  constructor(
    private request:RequestUtil,
    private formUtil : FormUtil
  ) { }

  //添加站点设备
  siteMonitorAdd(siteDevice : SiteDevices):Observable<MsgResult>{
    let params = this.formUtil.serialize(siteDevice);
    return this.request.post(SiteV2API.addSiteDevice,params);
  }

  //删除站点设备
  siteMonitorDelete(id : string):Observable<MsgResult>{
    return this.request.delete(SiteV2API.editSiteDevice.replace('{id}', id));
  }

  //批量删除站点设备
  siteMonitorDeletes(ids : Array<string>):Observable<MsgResult>{
    let params="ids="+ids;
    return this.request.post(SiteV2API.deleteSiteDevices,params);
  }

  getSiteMonitorById(id : string):Observable<MsgResult>{
    return this.request.get(SiteV2API.getSiteDeviceById.replace('{id}', id));
  }

  //修改站点设备
  siteMonitorEdit(siteDevice : SiteDevices):Observable<MsgResult>{
    let params = this.formUtil.serialize(siteDevice);
    return this.request.put(SiteV2API.editSiteDevice.replace('{id}', siteDevice.id),params);
  }

  //修改站点设备
  assignSite(params,id):Observable<MsgResult>{
    return this.request.put(SiteV2API.editSiteDevice.replace('{id}', id),params);
  }

  /**
   * 获取设备类型
   * @returns {Observable<MsgResult>|any}
   */
  getMonitor():Observable<MsgResult>{
    return this.request.get(SiteV2API.getMonitor);
  }


  /**
   * 保存或修改 为设备分配的权重
   * @param param 参数
   * @param snCode 设备编号
   * @returns {Observable<MsgResult>|any}
   */
  siteMonitorSetAddOrEdit(param ,snCode :string):Observable<MsgResult>{
    let params=param+"&snCode="+snCode;
    return this.request.post(SiteV2API.siteMonitorSetAddOrEdit,params);
  }

  /**
   * 删除某设备权重
   * @param id 主键
   * @returns {Observable<MsgResult>|any}
   */
  siteMonitorSetDelete(id :string):Observable<MsgResult>{
    return this.request.delete(SiteV2API.siteMonitorSetDelete+"/"+id+"?format=json");
  }
  /**
   * 获取所有站点类型
   * @returns {Observable<MsgResult>|any}
   */
  getAllSiteCate(): Observable<MsgResult> {
    return this.request.get(SiteV2API.getAllSiteCate);
  }
}
